<?php
/**
 * This is Areas controller test file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.tests.cases.controllers
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * AreasControllerTestCase class
 *
 * @package       lil
 * @subpackage    lil.tests.cases.controllers
 */
class AreasControllerTestCase extends ControllerTestCase {
/**
 * fixtures property
 *
 * @var array
 */
	public $fixtures = array(
		'plugin.lil.area', 'plugin.lil.areas_user', 'plugin.lil.user'
	);
/**
 * setUp method
 *
 * @return void
 */
	public function setUp() {
		parent::setUp();
		// reset variables
		require dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'Config' . DS . 'core.php';
	}
/**
 * tearDown method
 *
 * @return void
 */
	public function tearDown() {
		ClassRegistry::flush();
		parent::tearDown();
	}
/**
 * testIndex method
 *
 * @return void
 */
	public function testIndex() {
		$Areas = $this->generate('Lil.Areas');
		$this->testAction('lil/areas/index');
		$this->assertTrue(empty($this->headers['Location']));
	}
/**
 * testIndexSingleArea method
 *
 * When having single area in areas table, index action should redirect to this area
 *
 * @return void
 */
	public function testIndexSingleArea() {
		$Area = ClassRegistry::init('Lil.Area');
		$Area->deleteAll(array('Area.id <>' => 1));
		
		$Areas = $this->generate('Lil.Areas');
		$this->testAction('lil/areas/index');
		
		$this->assertTrue(!empty($this->headers['Location']));
		$expected_url = Router::url(array(
			'admin'      => false,
			'plugin'     => 'lil',
			'controller' => 'areas',
			'action'     => 'index',
			'area'       => 'first'
		), true);
		$this->assertEquals($this->headers['Location'], $expected_url);
	}
/**
 * testView method
 *
 * @return void
 */
	public function testView() {
		$Areas = $this->generate('Lil.Areas');
		$this->testAction('lil/areas/view/first');
	}
/**
 * testAdminIndex method
 *
 * @return void
 */
	public function testAdminIndex() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array(
				'Session'
			),
			'methods' => array(
				'isAuthorized',
				'redirect'
			),
		));
		$this->testAction('admin/lil/areas/index');
	}
/**
 * testAdminAdd method
 *
 * @return void
 */
	public function testAdminAdd() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash'),
		));
		
		$Areas->expects($this->once())->method('setFlash');
		
		$this->testAction('admin/lil/areas/add', array(
			'data' => array(
				'Area' => array(
					'name'        => 'My First Area added from Testsuite',
					'slug'        => 'my_first_area_added_from_testsuite',
					'description' => 'This is my first area added from Testsuite.'
				)
			)
		));
		
		$this->assertTrue(!empty($this->headers['Location']));
		$expected_url = Router::url(array(
			'admin'      => true,
			'plugin'     => 'lil',
			'controller' => 'areas',
			'action'     => 'admin_index'
		), true);
		$this->assertEqual($this->headers['Location'], $expected_url);
	}
/**
 * testAdminAddWithError method
 *
 * @return void
 */
	public function testAdminAddWithError() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods'    => array('isAuthorized', 'setFlash', 'callPluginHandlers'),
			'models'     => array( // do not allow plugin overrides
				'User'      => array('callPluginHandlers'),
				'AreasUser' => array('callPluginHandlers'),
				'Area'      => array('callPluginHandlers')
			) 
		));
		
		$Areas->expects($this->once())->method('setFlash')->with($this->anything(), $this->equalTo('error'));
		
		$this->testAction('admin/lil/areas/add', array(
			'data' => array(
				'Area' => array(
					'name'        => '',
					'slug'        => 'my_first_area_added_from_testsuite',
					'description' => 'This is my first area added from Testsuite.'
				)
			)
		));
		$this->assertTrue(empty($this->headers['Location']));
	}
/**
 * testAdminEdit method
 *
 * @return void
 */
	public function testAdminEdit() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash'),
		));
		
		$Areas->expects($this->once())->method('setFlash');
		
		$Area = ClassRegistry::init('Lil.Area');
		$this->assertEquals('My First Area', $Area->field('name', array('Area.id' => 1)));
		
		$this->testAction('admin/lil/areas/edit/1', array(
			'data' => array(
				'Area' => array(
					'id'   => 1,
					'name' => 'My First Area edited from Testsuite',
					Configure::read('Lil.areaSlugField') => 'test',
					'description' => 'This is my first area edited from Testsuite.'
				)
			)
		));
		
		$this->assertTrue(!empty($this->headers['Location']));
		$expected_url = Router::url(array(
			'admin'      => true,
			'plugin'     => 'lil',
			'controller' => 'areas',
			'action'     => 'admin_index'
		), true);
		$this->assertEquals($this->headers['Location'], $expected_url);
		
		$this->assertEquals('My First Area edited from Testsuite', $Area->field('name', array('Area.id' => 1)));
	}
/**
 * testAdminEditWithError method
 *
 * @return void
 */
	public function testAdminEditWithError() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods'    => array('isAuthorized', 'setFlash', 'callPluginHandlers'),
			'models'     => array( // do not allow plugin overrides
				'User'      => array('callPluginHandlers'),
				'AreasUser' => array('callPluginHandlers'),
				'Area'      => array('callPluginHandlers')
			) 
		));
		
		$Areas->expects($this->once())->method('setFlash')->with($this->anything(), $this->equalTo('error'));
		
		$this->testAction('admin/lil/areas/add', array(
			'data' => array(
				'Area' => array(
					'id'   => 1,
					'name' => '',
					Configure::read('Lil.areaSlugField') => 'test',
					'description' => 'This is my first area edited from Testsuite.'
				)
			)
		));
		$this->assertTrue(empty($this->headers['Location']));
	}
/**
 * testAdminDelete method
 *
 * @return void
 */
	public function testAdminDelete() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash'),
		));
		
		$Areas->expects($this->once())->method('setFlash');
		
		$Area = ClassRegistry::init('Lil.Area');
		$this->assertTrue($Area->hasAny(array('Area.id' => 1)));
		
		$this->testAction('admin/lil/areas/delete/1');
		
		$this->assertTrue(!empty($this->headers['Location']));
		
		$expected_url = Router::url(array(
			'admin'      => true,
			'plugin'     => 'lil',
			'controller' => 'areas',
			'action'     => 'list'
		), true);
		$this->assertEquals($this->headers['Location'], $expected_url);
		
		$this->assertFalse($Area->hasAny(array('Area.id' => 1)));
	}
/**
 * testAdminSelect method
 *
 * @return void
 */
	public function testAdminSelect() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('error404', 'isAuthorized', 'setFlash'),
		));
		
		$Areas->expects($this->once())->method('setFlash');
		$this->testAction('admin/lil/areas/select/1');
		$this->assertTrue(!empty($this->headers['Location']));
		$this->assertEquals($this->controller->currentArea->get('id'), 1);
	}
/**
 * testAdminSelectInvalid method
 *
 * @return void
 */
	public function testAdminSelectInvalid() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('error404', 'isAuthorized', 'setFlash'),
		));
		
		$Areas->expects($this->once())->method('error404');
		$this->testAction('admin/lil/areas/select/-30');
	}
/**
 * testAdminList method
 *
 * @return void
 */
	public function testAdminList() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash', 'error404'),
		));
		
		$this->testAction('admin/lil/areas/list');
		$this->assertTrue(empty($this->headers['Location']));
	}
/**
 * testIsAuthorized method
 *
 * @return void
 */
	public function testIsAuthorized() {
		$Areas = $this->generate('Lil.Areas', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
		));
		
		// stub out user
		$this->controller->currentUser = $this->getMock('LilCurrentUser');
		$this->controller->currentUser->expects($this->any())->method('canAccessArea')->will($this->onConsecutiveCalls(false, true));
		$this->controller->currentUser->expects($this->any())->method('role')->will($this->onConsecutiveCalls(true, false));
		
		$this->controller->request->params = array(
			'controller' => 'areas',
			'action' => 'admin_select',
			'pass' => array(1)
		);
		
		$result = $Areas->isAuthorized();
		$this->assertFalse($result);
		
		// authenticate as user 1
		//$this->Areas->Auth->login('1');
		
		$result = $Areas->isAuthorized();
		$this->assertTrue($result);
		
		unset($this->controller->request->params['pass'][0]);
		$result = $Areas->isAuthorized();
		$this->assertTrue($result);
		
		$this->controller->request->params['action'] = 'admin_add';
		$result = $Areas->isAuthorized();
		$this->assertTrue($result);
		
		// authenticate as user 2
		//$Areas->Auth->login('2');
		
		$result = $Areas->isAuthorized();
		$this->assertFalse($result);
	}
}